package servlet;

import bean.Arrence;
import utils.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/list")
public class ListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        查询数据库
        String sql = "select * from attence a,student s where a.sid = s.sid order by aid";
        ResultSet rs = DBUtil.query(sql);
//        设置一个集合
        ArrayList<Arrence> list = new ArrayList<>();
//        将结果封装到集合
        try {
            while(rs.next()) {
                int aid = rs.getInt("aid");
                String time = rs.getString("time");
                int type = rs.getInt("type");
                int sid = rs.getInt("sid");
                String sname = rs.getString("sname");
                Arrence arrence = new Arrence(aid, time, type, sid, sname);
                list.add(arrence);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        将集合添加到 request 域中
        request.setAttribute("list",list);
//        请求转发给一个JSP
        request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
